package com.example.demo.controller;

import com.example.demo.config.AppConfig;
import com.example.demo.service.DemoBeanService;
import com.example.demo.service.GreetingService;
import org.springframework.beans.factory.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.context.ConfigurableApplicationContext;

@RestController
@RequestMapping("/api/set")
public class SetController {
    private final DemoBeanService demoBeanService;
    private final GreetingService greetingService;


    @Autowired
    public SetController(
            DemoBeanService demoBeanService,
                         ConfigurableApplicationContext context,
            GreetingService greetingService) {
        var bean = context.getBean(
                AppConfig.class
        );
//        System.out.println("configurableApplicationContext bean is " + bean);
        //configurableApplicationContext bean is com.example.demo.config.AppConfig$$SpringCGLIB$$0@46eb2260


        System.out.println("this is setController " + bean.greetingService().sayHello());
        //this is setController this is a greeting message
        this.demoBeanService = demoBeanService;

        System.out.println("SetController created");


        this.greetingService = greetingService;
    }

    @RequestMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }

    @GetMapping("/demoBean")
    public void getDemoBean() {
        demoBeanService.displayMessage();

        // 这个是拿不到
        AppConfig appConfig = new AppConfig();
        var msg = appConfig.greetingService().sayHello();
        System.out.println(msg);
        //demo service message!
        //Greeting message: null
        //null


        // constructor greetingService
        System.out.println("construct service message: " + greetingService.sayHello());
        //construct service message: this is a greeting message
    }


}
